# Build script to compose the Live CD image.
#
# Copyright (c) 2017 MMPG.
# Copyright (c) 2017-2020 Matias Fonzo <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# ISO file name
ISONAME="${ISONAME:-dragora-3.0_$(date +%Y%m%d)-${arch}}"

# Default mkisofs(8) invocation name
MKISOFS=mkisofs

# Perform sanity checks (requirements)

if ! test -e "${rootdir}/squashfs-tools_lzip/mksquashfs"
then
    echo "Local \`${rootdir}/squashfs-tools_lzip/mksquashfs' does not exist." 1>&2
    exit 2;
fi
if ! type mkisofs > /dev/null
then
    echo "" 1>&2
    echo "\`mkisofs' is not found in your PATH." 1>&2
    if type genisoimage > /dev/null
    then
        MKISOFS=genisoimage
    else
        echo "\`genisoimage' was not found in your PATH." 1>&2
        echo "" 1>&2
        echo "Please install one of these programs to make the ISO image." 1>&2
        echo "" 1>&2
        exit 2;
    fi
fi

# This depends on the existence of the stage 1

if test ! -d "${output}/stage1/usr/pkg"
then
    echo "The stage number 1 does not exist or is incomplete." 1>&2
    echo "Build (completely) the stage 1 before processing '$stage'." 1>&2
    exit 1;
fi
if test ! -f "${rootdir}/initrd/initrd.img"
then
    echo "${rootdir}/initrd/initrd.img: No such initramfs file" 1>&2
    exit 1;
fi

STAGE1="${output}/stage1"
CDROM="${rootdir}/cdrom"

# Re-create CD tree

echo "Re-creating CD tree (${CDROM}) ..."
rm -rf -- "$CDROM"
mkdir -p -- "${CDROM}/isolinux"

# Copy kernel installation to isolinux/

cd -- "${STAGE1}/boot"

for file in System.map-* config-* vmlinuz-*
do
    cp -p "${STAGE1}/boot/$file" "${CDROM}/isolinux/${file%%-*}"
done
unset file

# Copy local isolinux files and initramfs for ISOLINUX

cp -p "${worktree}"/archive/isolinux/* "${CDROM}/isolinux/"
rm -f "${CDROM}/isolinux/isohybrid.pl" \
      "${CDROM}/isolinux/generate-keymap"

cp -p "${rootdir}/initrd/initrd.img" "${CDROM}/isolinux/"

# Produce compressed image using mksquashfs(1)
cd -- "$STAGE1"
rm -f ./dragora.sfs

"${rootdir}"/squashfs-tools_lzip/mksquashfs ./* "${CDROM}/dragora.sfs" \
 -comp lzip -b 128K -Xcompression-level 0 -Xdict-size 100% \
 -noappend -e ./tools ./var/cache/qi/packages ./root/.ash_history \
  ./*-log* ./usr/pkg/kernel-buildtree-* ./usr/src/linux-* ./usr/src/qi

# Produce hybrid ISO-9660 image

cd -- "$CDROM"

rm -f ./${ISONAME}*
$MKISOFS -v -o ${ISONAME}.iso                                     \
         -J -R -iso-level 3                                       \
         -no-emul-boot -boot-load-size 4 -boot-info-table         \
         -b "isolinux/isolinux.bin"                               \
         -c "isolinux/boot.cat"                                   \
         -V "Dragora-${arch}"                                     \
         .
unset MKISOFS

echo "Applying \"hybrid booting\" to the ISO image ..."
"${worktree}"/archive/isolinux/isohybrid.pl ${ISONAME}.iso

echo "Making .sha256 sum ..."
sha256sum ${ISONAME}.iso > ${ISONAME}.iso.sha256

echo "Finish ..."
echo ""
echo "***"
echo "The ISO image has been produced at"
echo "    ${CDROM}/${ISONAME}.iso"
echo "***"
echo ""
exit 0

